package sslh;

/** 
 ��datagrid����Ϊexcel�ļ�
*/
public class Exports
{
	private String _fileType; //�ļ�����
	private String _splitType; //�ָ��

	/** 
	 Ĭ�Ϲ��캯��������csv�ļ�
	*/
	public Exports()
	{
		_fileType = ".csv";
		_splitType = ",";
	}

	/** 
	 �������Ĺ��캯��
	 
	 @param type type=xls��csv
	*/
	public Exports(String type)
	{
		if (type.toLowerCase().equals("xls"))
		{
			_fileType = ".xls";
			_splitType = "\t";
		}
		else
		{
			_fileType = ".csv";
			_splitType = ",";
		}
	}

	/** 
	 ��ָ��ҳ�浼��DataGrid���ļ�
	 
	 @param pgPage ��Ҫ����DataGrid����ҳ
	 @param dgdData Ҫ�������ݵ�DataGrid
	 @param iFromCol Ҫ�����Ŀ�ʼ��
	 @param iToCol Ҫ�����Ľ����У�ȫ����������Ϊ0��
	 @param sFileName Ҫ�������ļ���
	 @param bOnlyVisuableCol �Ƿ�ֻ�����ɼ���
	 @param sTitle Ҫ��ӵı���
	*/
	public final void ExportDgToFile(System.Web.UI.Page pgPage, DataGrid dgdData, int iFromCol, int iToCol, String sFileName, boolean bOnlyVisuableCol, String sTitle)
	{
		String sw = "";

		if (sFileName.equals(""))
		{
			sFileName = java.time.LocalDateTime.Today.toString();
		}

		if (0 == iToCol)
		{
			iToCol = dgdData.Columns.size() - 1;
		}

		sw = CreateDgToFile(dgdData, iFromCol, iToCol, sFileName, bOnlyVisuableCol, sTitle);

		if (sw.equals(""))
		{
			sw = "<script language=javascript>alert('δ�ҵ��κοɵ��������ݣ���ʱ���ܵ�����')</script>";
			pgPage.RegisterStartupScript("script1",sw);
		}
		else
		{
			pgPage.Response.AddHeader("Content-Disposition", "attachment; filename=" + System.Web.HttpUtility.UrlEncode(sFileName,System.Text.Encoding.UTF8) + _fileType);
			pgPage.Response.ContentType = "application/ms-excel";
			pgPage.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312"); //UTF-8;
			pgPage.Response.Write(sw);
			pgPage.Response.End();
		}
	}

	/** 
	 �����ļ��ı�׼����
	 
	 @param dgdData Ҫ�������ݵ�DataGrid
	 @param iFromCol Ҫ�����Ŀ�ʼ��
	 @param iToCol Ҫ�����Ľ�����
	 @param sFileName Ҫ�������ļ���
	 @param bOnlyVisuableCol �Ƿ�ֻ�����ɼ���
	 @param sTitle Ҫ��ӵı���
	 @return ���ɵ��ַ���
	*/
	public final String CreateDgToFile(DataGrid dgdData, int iFromCol, int iToCol, String sFileName, boolean bOnlyVisuableCol, String sTitle)
	{
		StringWriter sw = new StringWriter(); //�ַ�����
		String LineStr = ""; //�õ���ÿһ���ַ���
		String sCellText = "";
		String sItem = "";
		java.lang.Class ColumnType;
		boolean bValidColumn;

		if ((0 == dgdData.Items.size()) || (0 == dgdData.Columns.size()))
		{
			return "";
		}

		// ----------------------�������ݽ��е���--------------------
		//�鿴ԭ�ؼ��Ƿ����
		boolean bAllowPage = dgdData.AllowCustomPaging;

		//����datagrid����ҳ���ܵ���ȫ������
		if (bAllowPage)
		{
			dgdData.AllowPaging = false;
			dgdData.DataBind();
		}

		//д��������
		if (!sTitle.equals(""))
		{
			sw.WriteLine(sTitle);
		}

		int i,j;

		//д�������
		for (j = iFromCol; j <= iToCol; j++)
		{
			if (!bOnlyVisuableCol || (bOnlyVisuableCol && dgdData.Columns[j].Visible))
			{
				//�Ƿ�Ϸ��Ŀɵ�����
				bValidColumn = false;

				//�԰�ť���в���������
				ColumnType = dgdData.Columns[j].getClass();

				//����
				if (ColumnType == BoundColumn.class)
				{
					bValidColumn = true;
				}

				//��������
				if (ColumnType == HyperLinkColumn.class)
				{
					bValidColumn = true;
				}

				//ģ����,label
				if (ColumnType == TemplateColumn.class)
				{
					for (Control ctl : dgdData.Items[0].Cells[j].Controls)
					{
						if (ctl.getClass() == Label.class)
						{
							bValidColumn = true;
							break;
						}
					}
				}

				//ģ����,TextBox
				if (ColumnType == TemplateColumn.class)
				{
					for (Control ctl : dgdData.Items[0].Cells[j].Controls)
					{
						if (ctl.getClass() == TextBox.class)
						{
							bValidColumn = true;
							break;
						}
					}
				}

				//�ɵ������򵼳�
				if (bValidColumn)
				{
					LineStr += ClearHtmlCont(dgdData.Columns[j].HeaderText) + _splitType;
				}
			}
		}
		LineStr = LineStr.substring(0, LineStr.length() - 1);
		sw.WriteLine(LineStr);

		//д������
		for (i = 0; i < dgdData.Items.size(); i++)
		{
			LineStr = "";
			for (j = iFromCol; j <= iToCol; j++)
			{
				if (!bOnlyVisuableCol || (bOnlyVisuableCol && dgdData.Columns[j].Visible))
				{
					//�Ƿ�Ϸ��Ŀɵ�����
					bValidColumn = false;

					//�԰�ť���в���������
					ColumnType = dgdData.Columns[j].getClass();

					//����
					if (ColumnType == BoundColumn.class)
					{
						sCellText = dgdData.Items[i].Cells[j].Text;
						bValidColumn = true;
					}

					//��������
					if (ColumnType == HyperLinkColumn.class)
					{
						for (Control ctl : dgdData.Items[i].Cells[j].Controls)
						{
							if (ctl.getClass() == HyperLink.class)
							{
								sCellText = ((HyperLink)ctl).Text;
								bValidColumn = true;
								break;
							}
						}
					}

					//ģ����,label
					if (ColumnType == TemplateColumn.class)
					{
						for (Control ctl : dgdData.Items[0].Cells[j].Controls)
						{
							if (ctl.getClass() == Label.class)
							{
								sCellText = ((Label)ctl).Text;
								bValidColumn = true;
								break;
							}
						}
					}

					//ģ����,TextBox
					if (ColumnType == TemplateColumn.class)
					{
						for (Control ctl : dgdData.Items[0].Cells[j].Controls)
						{
							if (ctl.getClass() == TextBox.class)
							{
								sCellText = ((TextBox)ctl).Text;
								bValidColumn = true;
								break;
							}
						}
					}

					//�ɵ������򵼳�
					if (bValidColumn)
					{
						//���յ���ȥ��
						if (sCellText.equals("&nbsp;"))
						{
							sCellText = "";
						}
						//�ϲ���һ��
						if (0 == j)
						{
							if (sCellText.equals(sItem))
							{
								sCellText = "";
							}
							else
							{
								sItem = sCellText;
							}
						}
						LineStr += ClearHtmlCont(sCellText.replace("\r"," ")) + _splitType;
					}
				}
			}
			LineStr = LineStr.substring(0, LineStr.length() - 1);
			sw.WriteLine(LineStr);
		}


		//����ҳ�ŵ�����
		if (dgdData.ShowFooter)
		{
			LineStr = "";
			for (j = iFromCol; j <= iToCol; j++)
			{
				LineStr += ClearHtmlCont(dgdData.Columns[j].FooterText) + _splitType;
			}

			LineStr = LineStr.substring(0, LineStr.length() - 1);
			sw.WriteLine(LineStr);
		}

		sw.Close();

		//����datagrid�ָ���ҳ
		if (bAllowPage)
		{
			dgdData.AllowPaging = true;
			dgdData.DataBind();
		}

		return sw.toString();
	}


	/** 
	 ��������������
	 
	 @param sCont ��������ַ���
	 @return �������ַ���
	*/
	public final String ClearHtmlCont(String sCont)
	{
		int iStart; //��ʼλ��
		int iEnd; //����λ��
		iStart = sCont.indexOf("<");
		if (iStart >= 0)
		{
			iEnd = sCont.indexOf(">");
			if (iEnd > 0)
			{
				sCont = tangible.DotNetToJavaStringHelper.remove(sCont, iStart, (iEnd + 1) - iStart);
				return ClearHtmlCont(sCont);
			}
		}
		return sCont;
	}
	//----------------------------------------------------------------
}